var Shop = function()
{
    this.idAutoComplete = 'autocomplete';
    this.idListAutoComplete = 'listautocomplete';
    this.idImageFolders = 'image_folders';
    this.idListImages = 'list_images';
    this.idShowImage = 'showimage';
    this.divProductOptions = 'product_options';
    this.divRelated = 'relateds';
    this.divFreebies = 'freebies';
    this.idGroups = 'groups';
    this.idProductList = 'prodlist';
    //this.idProductslink = 'productlink';
    this.idViewPage = 'viewpage';
    this.formAddProduct = 'add_product';
    this.idSubmitForm = 'submitform';
    this.idMessage = 'message';
    this.idBrandsAutoComplete = 'brandsautocomplete';
    this.idFilterProduct = 'filterproduct';
    this.idOnSale = 'onsale';
    this.idSalePrice = 'sale-price';
    this.idSaleDue = 'sale-expire';
    this.classListAutoComplete = 'listautocomplete';
    this.classFieldAutoComplete = 'autocomplete';
    this.messageForShowImage = '';
    //this.idTax = 'tax';
    this.countRelated = 3;
    this.countOptions = 3;
    this.separate = '###';
    this.webSiteUrl = '';
    this.products = '';
    this.brands = '';
	this.ajaxCache = {};
	this.lastXhr = null;
}
/*** init method ***/
Shop.prototype.init = function(params)
{
    this.webSiteUrl = params.websiteurl ? params.websiteurl : '';

	$('#'+this.idFilterProduct).bind({
		'keyup': function(){
			var val = $(this).val().toLowerCase();
			if (val) {
				$('ul#shopproducts li').each(function(){
				var text = $(this).text().toLowerCase();
				if (text.search(val) == -1) {
					$(this).hide();
				} else {
					$(this).show();
				}
			});
			} else {
				$('#prodlist li').show();
			}
		},
		'focus': function(){
			$(this).val('');
		},
		'blur': function(){
			$(this).val( $(this).attr('title') )
		}
	});

	$('#'+this.idGroups)
		.delegate('section.group-editable', 'focus', function(){
			$(this).text($(this).attr('title')).closest('div').css({border: '1px solid #7c7c7c'});
		})
		.delegate('section.group-editable', 'keyup', function(e) {
			if (null === e) {
				keyCode = event.keyCode;
			} else {
				keyCode = e.which;
			}
			if (keyCode == 13) {
				if ( $(this).text() === $(this).attr('title') ){
					return false;
				} else {
					var name = $(this).text();
					var id	  = $(this).attr('groupid');
					$.ajax({
						async: false,
						url: shop.webSiteUrl+'sys/backend_shopping/editgroup/',
						type: 'POST',
						data : {id:id, name: name},
						dataType: 'json',
						context: this,
						success : function(data) {
							if (data.save){
								$(this).attr('title', name);
							} else {
								$(this).text($(this).attr('title'));
							}
						},
						error: function(){$(this).text($(this).attr('title'));}
					});
				}
				$(this).blur();
			}
		})
		.delegate('section.group-editable', 'blur', function(){
			if ( $(this).text() === $(this).attr('title') ) {
				var text = $(this).text();
				$(this).text( text.substr(0, 12));
			} else {
				var text = $(this).attr('title');
				$(this).text( text.substr(0, 12));
			}
			$(this).closest('div').css({border: 'none'})
		});

	$('input.'+this.classFieldAutoComplete)
		.bind({
			'focus': function(){$(this).val('')},
			'blur': function(){$(this).val( $(this).attr('title') );}
		})
		.autocomplete({
			source: this.webSiteUrl+"sys/backend_shopping/getproducts/",
			select: function( event, ui ){
				if ( ui.item ) {
					var $list  = $(this).closest('div.tab').find('div.hooked_prods');
					var type = $($list).attr('id');
					if ( $list.find('p').size() < shop.countRelated ) {
						shop.addElementRelated(ui.item, type).appendTo($list);
					}
				}
				return false
			},
			focus: function( event, ui ) {
				return false;
			}
		});
	$('input.'+this.classFieldAutoComplete+':ui-autocomplete').each(function(){
		$(this).data( "autocomplete" )._renderItem = function( ul, item ){
			return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( '<a style="padding-left:50px"><img style="position:absolute; left: 5px;" width="40px" src="'+shop.webSiteUrl+'/'+item.img+'" /><p>' + item.label + '</p><p>Id: ' + item.sku + "</p></a>" )
			.appendTo( ul );
		};
	});

	$('input#brands-autocomplete').autocomplete({
		source: function( request, responce ){$.post(shop.webSiteUrl+'sys/backend_shopping/getbrands/', request, function(data){responce(data)}, 'json');},
		select: function( event, ui ){$(this).val(ui.item.value)}
	});

    this.messageForShowImage = document.getElementById(this.idShowImage).innerHTML;
}
/***  method using for ajax query ***/
Shop.prototype.ajax = function(params)
{
    var type = 'GET';
    var url = '';
    var async = true;
    var data = null;
    var dataType = 'html';
    if (params.type && params.type.toUpperCase() == 'POST') {
        type = params.type.toUpperCase()
    }
    if (params.url) {
        url = params.url;
    }
    if (params.data && typeof(params.data) == 'object') {
        data = '';
        for (var i in params.data) {
            var str = params.data[i].toString();
            var query = str.replace(/(&)/g, '');
            data += i+'='+query+'&';
            //data += i+'='+params.data[i]+'&';
        }
    }
    if (params.dataType) {
        dataType = params.dataType;
    }
    if (params.async == false) {
        async = false;
    }
    var req;
    try {req = new XMLHttpRequest();}
    catch (e) {
        try {req = new ActiveXObject("Msxml2.XMLHTTP");}
        catch (e) {
            try {req = new ActiveXObject("Microsoft.XMLHTTP");}
            catch (e) {return;}
        }
    }
    req.open(type, url, async);
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if (req.status == 200) {
                if (params.success) {
                    params.success(req.responseText, req.status);
                }
            }else {
                if (params.error) {
                    params.error(req.statusText, req.status);
                }
            }
        }
    }
    if (type == 'POST') {
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    } else {
        req.setRequestHeader('Content-Type', 'text/'+dataType+'');
    }
    req.setRequestHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT');
    req.setRequestHeader('Pragma', 'no-cache');
    req.send(data);
}
/*** method add product by product array ***/
Shop.prototype.addProductByProducts = function(data)
{
    this.products[this.products.length-1] = data;
}
/*** method loads images for selected folder and shows 'div' with those images ***/
Shop.prototype.loadImages = function(folder)
{
    if (folder == '') {return;}
    this.ajax({
        type : 'POST',
        url : this.webSiteUrl+'sys/backend_shopping/loadimages/',
        data : {folder : folder},
        success : function(data) {
            var obj = eval('('+data+')');
            var elm = document.getElementById(shop.idListImages);
            elm.innerHTML = obj.small;
            $(elm).show('700');
        }
    });
}
/*** method shows selected image and adds url of image in the form ***/
Shop.prototype.selectImage = function(src)
{
    var elm = document.getElementById(this.idShowImage);
    var img = new Image();
    img.onload = function () {
        elm.innerHTML = '';
        elm.appendChild(img);
    };
    img.src = src;
    var ie = (document.all && !window.opera);
    if (ie) {
        if (img.width > 140) {
            img.width = 140;
        }
        if (img.height > 130) {
            img.height = 130;
        }
    }
    document.getElementById(this.idListImages).style.display = 'none';
    document.forms[this.formAddProduct].image.value = src.replace(this.webSiteUrl, '');
}
/*** method adds options for product ***/
Shop.prototype.addOption = function()
{
    var children = document.getElementById(this.divProductOptions).childNodes;
    var counter = 0;
    for (var i=0; i<children.length; i++) {
        if (children.item(i).nodeName == "P") {
            counter++;
        }
    }
    if (counter < this.countOptions) {
        this.addElementOption('', '');
    }
}
/*** method adds options for product into html page ***/
Shop.prototype.addElementOption = function(key, val)
{
    var p = document.createElement('p');
    var del = document.createElement('img');
    del.setAttribute('title', 'delete this option');
    del.setAttribute('alt', 'delete option');
    del.setAttribute('onclick', 'this.parentNode.parentNode.removeChild(this.parentNode);');
    del.setAttribute('src', this.webSiteUrl+'system/images/ico_del_green.gif');
    var input1 = document.createElement('input');
    input1.setAttribute('type', 'text');
    input1.setAttribute('value', key);
    var input2 = document.createElement('input');
    input2.setAttribute('type', 'text');
    input2.setAttribute('value', val);
    p.appendChild(del);p.innerHTML += ' ';
    p.appendChild(input1);p.innerHTML += ' ';
    p.appendChild(input2);
    document.getElementById(this.divProductOptions).appendChild(p);
}
/*** method adds related products into html page ***/
Shop.prototype.addElementRelated = function(elementData, type)
{
	var newElem = $('<p/>')
				.attr('id', type+'-'+elementData.value)
				.append('<img src="'+shop.webSiteUrl+'system/images/ico_del_green.gif" title="delete" onclick="$(this).parent().remove()" style="cursor:pointer;position:absolute;top:0px;right:0px;width:12px;height:12px;"/>')
				.append('<img src="'+shop.webSiteUrl+'/'+elementData.img+'" width="110px;" />')
				.append('<span title="'+elementData.label+'">'+elementData.label.substr(0,40)+'</span>');
    return newElem;
}
/*** add group ***/
Shop.prototype.addGroup = function(name)
{
    name = name.replace(/^\s+/g,'');
    name = name.replace(/\s+$/g,'');
    if (name == '') {return;}
    this.ajax({
        type : 'POST',
        url : this.webSiteUrl+'sys/backend_shopping/addgroup/',
        data : {group : name},
        success : function(data) {
            var obj = eval('('+data+')');
            if (obj.exist) {alert('This group exist.');return;}
            if (obj.save) {
                shop.addElementGroup(obj.id, name);
            }
        }
    });
}
/*** add element group into html ***/
Shop.prototype.addElementGroup = function(id, name)
{
	var newGroup = $('<div/>')
		.append('<input type="checkbox" />')
		.append('<img title="delete '+name+'" src="'+this.webSiteUrl+'system/images/ico_delete.gif"')
		.append('<section title="'+name+'" contenteditable="true" class="group-editable">'+name.substr(0,11)+'</section>')
		.attr('id', 'group-'+id)
		.appendTo('#'+this.idGroups);
}
/*** delete group ***/
Shop.prototype.deleteGroup = function(id)
{
    if (!/^([0-9]{1,10})$/.test(id)) {return;}
    this.ajax({
        type : 'POST',
        url : this.webSiteUrl+'sys/backend_shopping/deletegroup/',
        data : {id : id},
        success : function(data) {
            var obj = eval('('+data+')');
            if (obj.del) {
                var elm = document.getElementById('group-'+id);
                elm.parentNode.removeChild(elm);
            }
        }
    });
}
/*** reset all checkbox for groups ***/
Shop.prototype.resetGroupsCheckbox = function()
{
    $('#'+this.idGroups).find('input:checkbox:checked').attr('checked', false);
}
/*** reset all value for form 'addproduct' ***/
Shop.prototype.resetFormAddProduct = function()
{
    $('#prodlist').hide();
    document.getElementById(this.idImageFolders).options[0].selected = true;
    document.getElementById(this.idShowImage).style.border = '0';
    document.getElementById(this.idShowImage).innerHTML = this.messageForShowImage;
    document.getElementById(this.idViewPage).style.display = 'none';
    document.getElementById(this.idViewPage).childNodes.item(1).href = '#';
    document.forms[this.formAddProduct].name.style.border = '1px solid #7c7c7c';
    document.forms[this.formAddProduct].item_id.style.border = '1px solid #7c7c7c';
    document.forms[this.formAddProduct].price.style.border = '1px solid #7c7c7c';
    document.forms[this.formAddProduct].weight.value = '';
    document.forms[this.formAddProduct].tax.value = 0;
    document.forms[this.formAddProduct].brand.value = '';
    document.forms[this.formAddProduct].image.value = '';
    document.forms[this.formAddProduct].id.value = 0;
    document.forms[this.formAddProduct].reset();
    document.getElementById('producttodel').value = '';
    document.getElementById(this.divProductOptions).innerHTML = '';
    this.addOption();
    document.getElementById(this.divRelated).innerHTML = '';
    document.getElementById(this.divFreebies).innerHTML = '';
    this.resetGroupsCheckbox();
    $('#onsale').attr('checked',false);
    $('.saleit').hide().find('input').val('');
    document.getElementById(this.idSubmitForm).setAttribute('onclick', 'shop.addProduct();');
}
/*** method returns options for product ***/
Shop.prototype.getOptions = function()
{
    var options = '';
    var listOptions = document.getElementById(this.divProductOptions).childNodes;
    for (var i=0; i<listOptions.length; i++) {
        if (listOptions.item(i).nodeName == "P") {
            var key = listOptions.item(i).childNodes.item(2).value;
            var val = listOptions.item(i).childNodes.item(4).value;
            key = key.replace(/^\s+/g,'');key = key.replace(/\s+$/g,'');
            val = val.replace(/^\s+/g,'');val = val.replace(/\s+$/g,'');
            if (key != '') {
                options += key+':::';
                options += val+this.separate;
            }

        }
    }
    return options;
}
/*** method returns related for product (return name of product)***/
Shop.prototype.getRelated = function()
{
    var related = '';
    var listRelated = document.getElementById(this.divRelated).childNodes;
    for (var i=0; i<listRelated.length; i++) {
        if (listRelated.item(i).nodeName == 'P') {
            var id = listRelated.item(i).id.split('-')[1];
             if (/^([0-9]{1,10})$/.test(id) && id > 0) {
                related += id+',';
             }
        }
    }
    return related;
}
/*** method returns freebies for product (name of freebies) ***/
Shop.prototype.getFreebies = function()
{
    var freebies = ''
    var listFreebies = document.getElementById(this.divFreebies).childNodes;
    for (var i=0; i<listFreebies.length; i++) {
        if (listFreebies.item(i).nodeName == 'P') {
            var id = listFreebies.item(i).id.split('-')[1];
            if (/^([0-9]{1,10})$/.test(id) && id > 0) {
                freebies += id+',';
            }
        }
    }
    return freebies;
}
/*** method returns groups for product (return id of group) ***/
Shop.prototype.getGroups = function()
{
    var groups = '';
    $('#'+this.idGroups).find('input:checkbox:checked').each(function(){
		groups += $(this).parent().attr('id').replace('group-','') + ',';
	});
    return groups;
}
/*** method sets options ***/
Shop.prototype.setOptions = function(data)
{
    document.getElementById(this.divProductOptions).innerHTML = '';
    var options = data.split(this.separate);
    for (var o in options) {
        var pair = options[o].split(':::');
        if (pair[0] != '') {
            this.addElementOption(pair[0], pair[1]);
        }
    }
}
/**
 * Method sets related/freebies for product (AJAX)
 */
Shop.prototype.setHookedProducts = function(data, type)
{
    var $list = $('#'+type);
    $list.html('');
	$.ajax({
		url: shop.webSiteUrl+'sys/backend_shopping/getproducts/',
		type: 'GET',
		data: {ids: data},
		dataType: 'json',
		success: function(data){
			for (var i in data) {
				shop.addElementRelated(data[i], type).appendTo($list);
			}
		},
		error: function(){}
		});
}
/*** method sets groups  ***/
Shop.prototype.setGroups = function(data)
{
    this.resetGroupsCheckbox();
    var groups = data.split(',');
	for (var g in groups) {
		if ( groups[g] > 0 && $('#group-'+groups[g]).size() ) {
			$('#group-'+groups[g]).find('input:checkbox').attr('checked', true);
		}
	}

}
/*** method checks form 'add_product' ***/
Shop.prototype.checkFormAddProduct = function()
{
    var ltrim = /^\s+/g, rtrim = /\s+$/g;
    var form = document.forms[this.formAddProduct];
    var name = form.name.value;
    var item_id = form.item_id.value;
    var price = form.price.value;
    var description = form.description.value;
    var image = form.image.value;

    name = name.replace(ltrim,'');name = name.replace(rtrim,'');
    item_id = item_id.replace(ltrim,'');item_id = item_id.replace(rtrim,'');
    description = description.replace(ltrim,'');description = description.replace(rtrim,'');
    image = image.replace(ltrim,'');image = image.replace(rtrim,'');

    var errors = new Array();
    if (name == '') {
        errors.push(1);form.name.style.border = '1px solid red';
    } else {
        form.name.style.border = '1px solid #7C7C7C';
    }
    if (item_id == '') {
        errors.push(1);form.item_id.style.border = '1px solid red';
    } else {
        form.item_id.style.border = '1px solid #7C7C7C';
    }
    if (!/^([0-9.]{1,10})$/.test(price)) {
        errors.push(1);form.price.style.border = '1px solid red';
    } else {
        form.price.style.border = '1px solid #7C7C7C';
    }
    if (image == '') {
        errors.push(1);document.getElementById(this.idShowImage).style.border = '1px solid red';
    } else {
        document.getElementById(this.idShowImage).style.border = '1px solid #7C7C7C';
    }
    if (errors.length > 0) {
        return false;
    }
    return true;
}
/*** method adds new product ***/
Shop.prototype.addProduct = function()
{
    if (!this.checkFormAddProduct()) {return;}
    var options  = this.getOptions();
    var related  = this.getRelated();
    var freebies = this.getFreebies();
    var groups   = this.getGroups();
    var form     = document.forms[this.formAddProduct];
    var msg      = document.getElementById(this.idMessage);
    var onSale   = $('#onsale').attr('checked')?1:0;
    var salePrice = $('#sale-price').val();
    var saleDue  = $('#sale-expire').val();
    this.ajax({
        type : 'POST',
        url : this.webSiteUrl+'sys/backend_shopping/addproduct/',
        data : {name:form.name.value, item_id:form.item_id.value, mpn:form.mpn.value, price:form.price.value, weight:form.weight.value, tax:form.tax.options[form.tax.selectedIndex].value, photo:form.image.value,
            brand:form.brand.value, desc:form.description.value, options:options, related:related, freebies:freebies, groups:groups, onsale:onSale, salePrice:salePrice, saleExp:saleDue, fullDesc:form.fullDescription.value},
        success : function(data) {
            var obj = eval('('+data+')');
            if (obj.id > 0 ) {
                shop.addProductByProducts(form.name.value+':::'+form.image.value+':::'+obj.id);
                var prodName = form.name.value;
                var prodItemId = form.item_id.value;
                prodName = prodName.replace(/^\s+/g,'');prodName = prodName.replace(/\s+$/g,'');
                prodItemId = prodItemId.replace(/^\s+/g,'');prodItemId = prodItemId.replace(/\s+$/g,'');
				var templateProduct = $('<li class="shoproductlisting" id="'+obj.id+'" />')
				var templateLink = $('<a href="#" onclick="shop.getDataProductForForm('+obj.id+');"></a>');
				templateLink = $(templateLink).append('<span class="shoproductlistingid">'+prodItemId+'</span>')
					.append('<img width="100px" src="'+shop.webSiteUrl+form.image.value+'" alt="'+prodItemId+'" />')
					.append('<span>'+(prodName.length < 24 ? prodName : prodName.substr(0,24)+'...')+'</span>');
				$(templateProduct).append(templateLink).appendTo('#shopproducts');
                msg.style.backgroundColor = 'green';
                msg.innerHTML = 'Product added. <a href="'+(obj.url.length > 0 && typeof obj.url == 'string' ? shop.webSiteUrl+obj.url+'.html' : '#')+'" target="blank" style="color:#fff;" onclick="this.parentNode.style.display=\'none\';">jump to page</a>';
                msg.style.display = 'block';
                shop.resetFormAddProduct();
            } else {
                msg.style.backgroundColor = 'red';
                msg.innerHTML = "Can't add this product.";
                msg.style.display = 'block';
            }
        },
        error : function(statusText) {
            msg.style.backgroundColor = 'red';
            msg.innerHTML = 'Error: '+statusText;
            msg.style.display = 'block';
        }
    });
}
/**
 * method loads data product
 */
Shop.prototype.getDataProductForForm = function(prodid)
{
    document.getElementById(shop.idProductList).style.display = 'none';
    var productId = prodid;
    if (!/^([0-9]{1,10})$/.test(productId)) {return;}
    this.ajax({
        type : 'POST',
        url : this.webSiteUrl+'sys/backend_shopping/getdataproduct/',
        data : {id : productId},
        success : function(data) {
            var product = eval('('+data+')');
            if (product.id > 0) {
                shop.resetFormAddProduct();
                document.getElementById('producttodel').value = productId;
                var form = document.forms[shop.formAddProduct];
                form.id.value = product.id;
                form.name.value = product.name;
                form.item_id.value = product.item_id;
                form.price.value = product.price;
                form.weight.value = product.weight;
                form.tax.value = product.tax;
                form.brand.value = product.brand;
                form.description.value = product.desc;
                form.salePrice.value = product.salePrice;
                form.saleExp.value = product.saleDue;
				form.mpn.value = product.mpn;
				form.fullDescription.value = product.fullDesc;
                form.onsale.checked = (product.isForSale) ? 'checked' : '';
                if(product.isForSale) {
                   $('.saleit').toggle();
                }
                if (product.photo != '') {
                    shop.selectImage(shop.webSiteUrl+product.photo);
                }
                if (product.options != '') {
                    shop.setOptions(product.options);
                }
                if (product.related != '') {
                   shop.setHookedProducts(product.related, 'relateds')
                }
                if (product.freebies != '') {
                    shop.setHookedProducts(product.freebies, 'freebies')
                }
                if (product.groups != '') {
                    shop.setGroups(product.groups);
                }
                if (product.pageurl.length > 0 && typeof product.pageurl == 'string') {
                    var viewLink = document.getElementById(shop.idViewPage);
                    viewLink.childNodes.item(1).href = shop.webSiteUrl+product.pageurl+'.html';
                    viewLink.childNodes.item(1).removeAttribute('onclick');
                    viewLink.style.display = 'block';
                }
                document.getElementById(shop.idSubmitForm).setAttribute('onclick', 'shop.editProduct();');
            }
        }
    });
}
/*** method deletes product by id ***/
Shop.prototype.deleteProduct = function(productId)
{
    if (!/^([0-9]{1,10})$/.test(productId)) {return;}
    this.ajax({
        type : 'POST',
        url : this.webSiteUrl+'sys/backend_shopping/deleteproduct/',
        data : {id : productId},
        success : function(data) {
            var obj = eval('('+data+')');
            var msg = document.getElementById(shop.idMessage);
            if (obj.del) {
                msg.innerHTML = 'Product deleted.';msg.style.display = 'block';
				$('ul#shopproducts li.shoproductlisting[id="'+productId+'"]').remove();
                shop.resetFormAddProduct();
            } else {
                msg.innerHTML = "Can't delete this product.";msg.style.display = 'block';
            }
        }
    });
}
/*** method edits a product ***/
Shop.prototype.editProduct = function()
{
    var form = document.forms[this.formAddProduct];
    if (!/^([0-9]{1,10})$/.test(form.id.value) || form.id.value <= 0) {return;}
    if (!this.checkFormAddProduct()) {return;}
    var options  = this.getOptions();
    var related  = this.getRelated();
    var freebies = this.getFreebies();
    var groups = this.getGroups();
    var msg = document.getElementById(this.idMessage);
    var onSale = $('#onsale').attr('checked')?1:0;
    var salePrice = $('#sale-price').val();
    var saleDue = $('#sale-expire').val();
	var tax = (form.tax.options.length > 0) ? form.tax.options[form.tax.selectedIndex].value : "";
    this.ajax({
        type : 'POST',
        url : this.webSiteUrl+'sys/backend_shopping/editproduct/',
        data : {
			id        : form.id.value,
			name      : form.name.value,
			item_id   : form.item_id.value,
			price     : form.price.value,
			weight    : form.weight.value,
			tax       : tax,
			photo     : form.image.value,
            brand     : form.brand.value,
			desc      : form.description.value,
			options   : options,
			related   : related,
			freebies  : freebies,
			groups    : groups,
			onsale    : onSale,
			salePrice : salePrice,
			saleExp   : saleDue,
			mpn       : form.mpn.value,
			fullDesc  : form.fullDescription.value
		},
        success : function(data) {
            var obj = eval('('+data+')');
            if (obj.save) {
                msg.style.backgroundColor = 'green';
                msg.innerHTML = 'Product updated.';
                msg.style.display = 'block';
                var prodName = form.name.value;
                var prodItemId = form.item_id.value;
                prodName = prodName.replace(/^\s+/g,'');prodName = prodName.replace(/\s+$/g,'');
                prodItemId = prodItemId.replace(/^\s+/g,'');prodItemId = prodItemId.replace(/\s+$/g,'');
                var templateProduct = '<a href="#" onclick="shop.getDataProductForForm('+form.id.value+');">';
                templateProduct += '<span class="shoproductlistingid">'+prodItemId+'</span>';
                templateProduct += '<img width="100px" src="'+shop.webSiteUrl+form.image.value+'" alt="'+prodItemId+'" />';
                templateProduct += '<span>'+(prodName.length < 24 ? prodName : prodName.substr(0,24)+'...')+'</span></a>';
                document.getElementById(form.id.value).innerHTML = templateProduct;
            } else {
                msg.style.backgroundColor = 'red';
                msg.innerHTML = "Can't update this product.";
                msg.style.display = 'block';
            }
        },
        error : function(statusText) {
            msg.style.backgroundColor = 'red';
            msg.innerHTML = 'Error: '+statusText;
            msg.style.display = 'block';
        }
    });
}

var shop = new Shop();